#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# Copyright 2013 Antoine Drouin (poinix@gmail.com)
#
# This file is part of PAT.
#
#    PAT is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    PAT is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PAT.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Demonstrates a run of the rotorcraft Hudson/Khan dynamic model 
"""

import numpy as np
import matplotlib.pyplot as plt

import pat.vehicles.rotorcraft.dynamic_model_hk as dm
import pat.vehicles.rotorcraft.autopilot_hk     as ap

import pat.utils as pu

my_dm = dm.DynamicModel()
my_ap = ap.Autopilot(my_dm)

Xe, Ue = my_dm.trim()
#my_ap.set_ref(ap.Step_x(Xe, Ue))

X0 = np.array(Xe)
my_dm.reset(X0)

dt = 0.01; time = np.arange(0., 30., dt)
X = np.zeros((len(time), my_dm.sv_size)); X[0] = X0
U = np.zeros((len(time), my_dm.iv_size))
for i in range(1,len(time)):

#    if i == int(len(time)*0.5):
#        my_ap.set_cur_mode(0)
#        my_ap.set_ref(ap.Step_phi(Xe, Ue))
#        print "hello"
        
    U[i-1], refi = my_ap.run(time[i-1], X[i-1])
    X[i] = my_dm.run(dt, U[i-1])

U[-1]=U[-2]

my_dm.plot_trajectory(time, X, U)
plt.show()
